package com.catmiw.swt;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;

public abstract class XMLBean {
	protected String xmlname;

	public String toString(){
		StringBuilder sb=new StringBuilder();
		sb.append("<"+xmlname+">\r\n");
	
		PropertyDescriptor[] pds=PropertyUtils.getPropertyDescriptors(this);
		
		for (PropertyDescriptor pd:pds){
			if ("class".equals(pd.getName())){
				continue;
			}

			Object o=null;
			try {
				o = PropertyUtils.getProperty(this, pd.getName());
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
			if (o!=null){
				if (o instanceof List){
					sb.append("<"+pd.getName()+">");
					List<?> list=(List<?>)o;
					for (Object oo:list){
						sb.append(oo.toString());
					}
					sb.append("</"+pd.getName()+">");
				} else {
					sb.insert(sb.indexOf(">",0), " "+pd.getName()+"=\""+o.toString()+"\" ");
				}
			}
		}
		sb.append("</"+xmlname+">\r\n");
	
		return sb.toString();

	}
}
